package com.kly.user.task;


import cn.hutool.core.net.NetUtil;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.kly.user.db.dao.IssueUserDao;
import com.kly.user.db.dao.UserBehaviorGpsDao;
import com.kly.user.db.dao.UserConversationDao;
import com.kly.user.db.dao.UserDao;
import com.kly.user.db.dbo.IssueUserDBO;
import com.kly.user.db.entity.IssueUserEntity;
import com.kly.user.db.entity.UserBehaviorGpsEntity;
import com.kly.user.db.entity.UserEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;

/**
 * model用户池
 */
@Component
public class IssueUserTask {

    private static Log logger = LogFactory.getLog(IssueUserTask.class);

    @Resource
    private UserDao userDao;

    @Resource
    private UserConversationDao userConversationDao;

    @Resource
    private IssueUserDao issueUserDao;

    @Resource
    private UserBehaviorGpsDao userBehaviorGpsDao;

    @NacosValue(value = "${task.job.executor.ip}", autoRefreshed = true)
    private String taskJobExecutor;

    @NacosValue(value = "${issueTask.mins:5}", autoRefreshed = true)
    private Integer issueTaskMins;

    private String localIP = NetUtil.getLocalhostStr();

    @Scheduled(fixedDelay = 1000 * 60)
    public void run1() {
        if (issueTaskMins == 1) {
            run(issueTaskMins);
        }
    }

    @Scheduled(fixedDelay = 1000 * 60*2)
    public void run2() {
        if (issueTaskMins == 2) {
            run(issueTaskMins);
        }
    }
    @Scheduled(fixedDelay = 1000 * 60*3)
    public void run3() {
        if (issueTaskMins == 3) {
            run(issueTaskMins);
        }
    }
    @Scheduled(fixedDelay = 1000 * 60*4)
    public void run4() {
        if (issueTaskMins == 4) {
            run(issueTaskMins);
        }
    }
    @Scheduled(fixedDelay = 1000 * 60*5)
    public void run5() {
        if (issueTaskMins == 2) {
            run(issueTaskMins);
        }
    }

    public void run(Integer mins) {
        logger.info("==IssueUserTask 当前选择的刷新时间是==,{}mins",issueTaskMins);
        if (!taskJobExecutor.contains(localIP)) {
            logger.info("=============IssueUserTask, currentLocalAddress:{}, onlyExecuteTaskIn:{}", localIP, taskJobExecutor);
            return;
        }

        List<UserEntity> cMOnlineList = userDao.queryCMOnline();
        for (UserEntity modelUser : cMOnlineList) {
            Calendar nowTime = Calendar.getInstance();
            nowTime.add(Calendar.MINUTE, -30);
            Integer conversationCount = userConversationDao.getDayConversationCount(modelUser.getUserCode(), nowTime.getTime());
            IssueUserDBO issueUserEntity = new IssueUserDBO();
            issueUserEntity.setModelUserCode(modelUser.getUserCode());
            issueUserEntity.setConnectCount(conversationCount);
            issueUserEntity.setGender(modelUser.getGender());
            issueUserEntity.setUseStatus(0);
            UserBehaviorGpsEntity userBehaviorGpsEntity = userBehaviorGpsDao.getLastGps(modelUser.getUserCode());
            if (userBehaviorGpsEntity != null) {
                issueUserEntity.setLat(userBehaviorGpsEntity.getLat());
                issueUserEntity.setLng(userBehaviorGpsEntity.getLng());
                issueUserDao.insertLocation(issueUserEntity);
            }
        }
        if (!cMOnlineList.isEmpty()) {
            //删除所有状态为1的数据
            issueUserDao.deleteNotUse();
            //更新状态为1
            issueUserDao.updateUseStatus();
        }
    }

}
